﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using ArchivesAdmin.Util;
using ArchivesAdmin.Util.Model;
using ArchivesAdmin.Entity;
using ArchivesAdmin.Model;
using ArchivesAdmin.Admin.Web.Controllers;
using ArchivesAdmin.Entity.StorageManage;
using ArchivesAdmin.Business.StorageManage;
using ArchivesAdmin.Model.Param.StorageManage;
using System.Text;
using ArchivesAdmin.Service;
using ArchivesAdmin.Service.StorageManage;
using System.Collections;

namespace ArchivesAdmin.Admin.Web.Areas.StorageManage.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-19 09:36
    /// 描 述：控制器类
    /// </summary>
    [Area("StorageManage")]
    public class BarLibController :  BaseController
    {
        private BarLibBLL barLibBLL = new BarLibBLL();

        #region 视图功能


        //[AuthorizeFilter("storage:barlib:view")]
        public ActionResult BarLibIndex()
        {
            return View();
        }
        [HttpPost]
        public async Task<ActionResult> GetReprintCodes(string frompage)
        {
            TData<string> obj = new TData<string>();

            var sb = new StringBuilder();
            var list = barLibBLL.GetReprintCodes(frompage);
            string[] str  ;
            foreach(var item in list.Result.ToList())
            {
                sb.Append("<div class=\"ibox\">");
                sb.Append("<div class=\"ibox-title  gray-bg\">");
                sb.Append("<a class=\"fa fa-print pull-right\" onclick=\"print('");
                if (frompage == "BarLib")
                    sb.Append(item.Bincode);
                else
                    sb.Append(item.Tempbincode);
                sb.Append("')\"></a>");
                sb.Append("<h5 class=\"font-bold\" onclick=\"print('");
                if (frompage == "BarLib")
                    sb.Append(item.Bincode);
                else
                    sb.Append(item.Tempbincode);
                sb.Append("')\">");
                if (frompage == "BarLib")
                    sb.Append(item.Bincode);
                else
                    sb.Append(item.Tempbincode);
                sb.Append("</h5>");
                sb.Append("</div>");

                str = item.Boxcode.Split(";");
                sb.Append("<div class=\"ibox-content  gray-bg\">");

                foreach (var s in str)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        sb.Append("<a class=\"btn btn-success btn-rounded\" onclick=\"print('");
                        sb.Append(s);
                        sb.Append("')\">");
                        sb.Append(s);
                        sb.Append("</a>"); 
                    }
                }
                sb.Append("</div>");

                sb.Append("</div>");
            }
            obj.Tag = 1;
            obj.Result =  sb.ToString();

            return Json(obj);

        }

        public ActionResult BarLibForm()
        {
            return View();
        }
        #endregion

        private string GetBoxCodesListNotMapped(TData<string> obj)
        {
            var sb = new StringBuilder();
            int count = 0;
            foreach (var item in obj.ResultList)
            {
                count++;
                sb.Append("<button type = 'button' class='btn btn-xs btn-info'>");
                sb.Append(item);
                sb.Append("</button>");
            }
            obj.TotalCount = count;

            return  sb.ToString();
        }
        #region 获取数据
        //[HttpGet]
        //[AuthorizeFilter("storage:barlib:search")]
        public async Task<ActionResult> GetListJson(BarLibListParam param)
        {
            TData<List<BarLibEntity>> obj = await barLibBLL.GetList(param);
            return Json(obj);
        }

        //[HttpGet]
        //[AuthorizeFilter("storage:barlib:search")]
        public async Task<ActionResult> GetPageListJson(BarLibListParam param, Pagination pagination)
        {
            TData<List<BarLibEntity>> obj = await barLibBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<BarLibEntity> obj = await barLibBLL.GetEntity(id);
            return Json(obj);
        }
        #endregion

        #region 提交数据
        
        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> ReprintCode(string code)
        {
            // 重打标签： 盒码 or  箱码
            TData<string> obj =  await barLibBLL.ReprintCode(code); 

            return Json(obj);
        }

        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> PrintBoxCode(BarLibPrintParam param)
        {
            // 打印盒码 --- for  档案封箱 页面           
            TData<string> obj = await barLibBLL.PrintBoxCode(param, "BarLib");
            string notmapped = GetBoxCodesListNotMapped(obj);

            obj.Description = notmapped;

            return Json(obj);
        }



        //created by XQ 2021-7-9
        [HttpPost]
      //  [AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> BatchPrintBoxCode(string boxCodes)
        {
            TData<string> obj = await barLibBLL.BatchPrintBoxCode(boxCodes);
            return Json(obj);                    
        }

        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> GetBoxCodesListNotFinished(BarLibPrintParam param, string frompage)
        {
            // for  档案封箱 页面
            TData<string> obj   = await  barLibBLL.GetBoxCodesListNotFinished(param, frompage);
            string notmapped = GetBoxCodesListNotMapped(obj);

            obj.Tag = 1;
            obj.Description = notmapped;

            return Json(obj);
        }

        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> PrintBinCode(BarLibPrintParam param)
        {
            // 打印箱码
            TData<string> obj = await barLibBLL.PrintBinCode(param);
            return Json(obj);
        }

        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> SetBinSecret(BarLibPrintParam param)
        {
            // 记录箱子的密码锁
            TData<string> obj = await barLibBLL.SetBinSecret(param);
            return Json(obj);
        }

        #endregion
    }
}
